/**
 * This file is part of the FlexiTimer project.
 * 
 * Author: Florian Arndt <f.aarndt@web.de>
 *  	   Clemens Teichmann <>
 *
 * http://code.google.com/p/flexitimer/ 
 */

package flexitimer_client.gui;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JSpinner;
import javax.swing.JTextField;
import javax.swing.SpinnerNumberModel;

import flexitimer_client.calc.activateOption;

public class optionFrame extends JFrame {

	private static optionFrame 	instance 			= null;
			JTextField 			maxTime 			= new JTextField("10:30");
			JTextField 			minTime 			= new JTextField("08:18");
			JLabel 				maxTimeInfo 		= new JLabel("MaxTime eingeben: ");
			JLabel 				minTimeInfo 		= new JLabel("MinTime eingeben: ");
			SpinnerNumberModel 	sdmInterval 		= new SpinnerNumberModel();
			JSpinner 			intervalSpinner 	= new JSpinner(sdmInterval);
			JLabel 				popupIntervalInfo 	= new JLabel("Popup Intervall: ");
			JLabel				popupIntervalMin	= new JLabel("min");
			JPanel 				panel 				= new JPanel();
			JButton 			button 				= new JButton(new activateOption());
	
	private optionFrame() {
		super("FlexiTimer - Option");
		initGUI();
	}
	
	private void initGUI() {
		setSize(300,125);
		setLayout(new BorderLayout());
		setResizable(true);
				
		sdmInterval.setMinimum(2);
		sdmInterval.setMaximum(60);
		sdmInterval.setValue(60);
		
		
		GridBagLayout myGridBagLayout = new GridBagLayout();
		GridBagConstraints c = new GridBagConstraints();
		c.fill	 = GridBagConstraints.BOTH;
		
		
		panel.setLayout(myGridBagLayout);
		
		c.gridx = 0;
		c.gridy = 0;
		panel.add(maxTimeInfo, c);
		c.gridx = 1;
		c.gridy = 0;
		maxTime.setPreferredSize(new Dimension(75,20));
		panel.add(maxTime, c);
		c.gridx = 0;
		c.gridy = 1;
		panel.add(minTimeInfo, c);
		c.gridx = 1;
		c.gridy = 1;
		minTime.setPreferredSize(new Dimension(75,20));
		panel.add(minTime, c);
		c.gridx = 0;
		c.gridy = 2;
		panel.add(popupIntervalInfo, c);
		c.gridx = 1;
		c.gridy = 2;
		intervalSpinner.setPreferredSize(new Dimension(75,20));
		panel.add(intervalSpinner, c);
		c.gridx = 2;
		c.gridy = 2;
		panel.add(popupIntervalMin, c);
		c.gridx = 0;
		c.gridy = 3;
		button.setPreferredSize(new Dimension(75,20));
		panel.add(button, c);
		

		add(panel, BorderLayout.CENTER);
	}
	
	public static optionFrame getInstance() {
		if (instance == null) {
			instance = new optionFrame();
		}
		return instance;
	}
	
	public String getMaxTime() {
		if(maxTime.getText().length() == 4) {
		String hour = maxTime.getText().substring(0, 2);
		String min  = maxTime.getText().substring(2, 4);
		return hour+":"+min;
	}
	if(maxTime.getText().charAt(2) == '.') {
		String hour = maxTime.getText().substring(0, 2);
		String min  = maxTime.getText().substring(3, 5);
		return hour+":"+min;
	}
	else { return maxTime.getText(); }
	}
	
	public String getMinTime() {
		if(minTime.getText().length() == 4) {
			String hour = minTime.getText().substring(0, 2);
			String min  = minTime.getText().substring(2, 4);
			return hour+":"+min;
		}
		if(minTime.getText().charAt(2) == '.') {
			String hour = minTime.getText().substring(0, 2);
			String min  = minTime.getText().substring(3, 5);
			return hour+":"+min;
		}
		else { return minTime.getText(); }
	}
	
	public int getPopupInterval() {
		if (Integer.parseInt(sdmInterval.getValue().toString()) == 60)
			return 0;
		return Integer.parseInt(sdmInterval.getValue().toString());
	}
}
